#!/usr/bin/env bash

### BEGIN ###
# Author: idevz
# Since: 2018/03/12
# Description:       Set up Base pvm
# ./init.sh          Set up using $(pwd) as BASE_DIR, down which has go source and binaries.
#
# Environment variables that control init:
# SIP: default static ip.
### END ###

set -ex

[ -z $PRLCTL_HOME ] && PRLCTL_HOME=/media/psf/runX
[ -z $RUNX_PKGS ] && RUNX_PKGS=/media/psf/runX-pkgs
STATIC_IP=${SIP:-"10.211.55.100"}
BASE_DIR=${BASE_DIR:-"$(readlink -f "$(dirname "$0")")"}
HOST_NAME_SET=${HNS:-"base"}
export THREADS=$(grep -c ^processor /proc/cpuinfo)

# manualset eth0
eth0_setting() {
	ETH0_CONF=$(
		cat <<ETH0
# Generated by parse-kickstart
DEVICE="eth0"
IPV6INIT="yes"
BOOTPROTO="static"
ONBOOT="yes"
IPADDR="${STATIC_IP}"
NETMASK="255.255.255.0"
GATEWAY="10.211.55.1"
ETH0
	)

	echo "${ETH0_CONF}" | sudo tee /etc/sysconfig/network-scripts/ifcfg-eth0
	sudo ifdown eth0 && sudo ifup eth0
}

pre_install_parallels_tools() {
	sudo yum -y install https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm dkms \
		ftp://ftp.pbone.net/mirror/ftp.scientificlinux.org/linux/scientific/7.2/x86_64/updates/security/kernel-devel-3.10.0-693.el7.x86_64.rpm
	mkdir ~/.ssh/
	chmod -R 700 ~/.ssh/
	sudo mkdir /root/.ssh/
	sudo chmod -R 700 /root/.ssh/
}

none_pass_login() {
	cp ~/.ssh/id_rsa.pub ~/.ssh/authorized_keys
	chmod 600 ~/.ssh/authorized_keys

	sudo cp /root/.ssh/id_rsa.pub /root/.ssh/authorized_keys
	sudo chmod 600 /root/.ssh/authorized_keys
}

# install_parallels_tools manual install
install_parallels_tools() {
	pre_install_parallels_tools
	[ ! -d /media/cdrom ] && sudo mkdir /media/cdrom
	sudo mount | grep iso9660
	sudo umount /dev/cdrom \
		mount -o exec /dev/cdrom /media/cdrom \
		cd /media/cdrom/ \
		./install
}

using_zsh() {
	none_pass_login
	sudo yum install -y zsh
	local zsh_location=$(which zsh)
	echo ${zsh_location} | sudo tee -a /etc/shells
	chsh -s ${zsh_location}
	sudo chsh -s ${zsh_location}
}

init_etc() {
	sudo hostnamectl set-hostname ${HOST_NAME_SET}
	using_zsh
	sudo cp "${PRLCTL_HOME}/shell/etc-sudoers" /etc/sudoers
	sudo cp "${PRLCTL_HOME}/shell/etc-profile" /etc/profile
	cp "${PRLCTL_HOME}/shell/runX.zshrc" ~/.zshrc
	sudo cp "${PRLCTL_HOME}/shell/runX.zshrc" ~/.zshrc
}

install_gdb() {
	[ ! -f "${BASE_DIR}/srcs/gdb-8.2.tar.gz" ] &&
		curl -fSL ftp://ftp.gnu.org/gnu/gdb/gdb-8.2.tar.gz -o ${BASE_DIR}/srcs/gdb-8.2.tar.gz
	sudo yum -y install gcc-c++ #configure: error: *** A compiler with support for C++11 language features is required.
	sudo yum install -y texinfo #makeinfo error when running make install
	[ ! -f "${BASE_DIR}/srcs/lzma-libs-4.32.7-1.el7.rf.x86_64.rpm" ] &&
		curl -fSL http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el7/en/x86_64/rpmforge/RPMS/lzma-libs-4.32.7-1.el7.rf.x86_64.rpm -o ${BASE_DIR}/srcs/lzma-libs-4.32.7-1.el7.rf.x86_64.rpm
	sudo yum --nogpgcheck -y localinstall ${BASE_DIR}/srcs/lzma-libs-4.32.7-1.el7.rf.x86_64.rpm
	[ ! -f "${BASE_DIR}/srcs/lzma-devel-4.32.7-1.el7.rf.x86_64.rpm" ] &&
		curl -fSL http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el7/en/x86_64/rpmforge/RPMS/lzma-devel-4.32.7-1.el7.rf.x86_64.rpm -o ${BASE_DIR}/srcs/lzma-devel-4.32.7-1.el7.rf.x86_64.rpm
	sudo yum --nogpgcheck -y localinstall ${BASE_DIR}/srcs/lzma-devel-4.32.7-1.el7.rf.x86_64.rpm
	[ ! -f "${BASE_DIR}/srcs/lzma-4.32.7-1.el7.rf.x86_64.rpm" ] &&
		curl -fSL http://ftp.tu-chemnitz.de/pub/linux/dag/redhat/el7/en/x86_64/rpmforge/RPMS/lzma-4.32.7-1.el7.rf.x86_64.rpm -o ${BASE_DIR}/srcs/lzma-4.32.7-1.el7.rf.x86_64.rpm
	sudo yum --nogpgcheck -y localinstall ${BASE_DIR}/srcs/lzma-4.32.7-1.el7.rf.x86_64.rpm

	# gdb cannot read .gnu_debugdata sections because gdb lacks LZMA support
	# not good after install three above, but after below, is ok
	sudo yum install -y xz xz-devel

	sudo yum install -y ncurses-devel           # for add TUI support

	sudo yum install -y python36-devel python36 # add python support
	cd "${BASE_DIR}/srcs/"
	[ -d "${BASE_DIR}/srcs/gdb-8.2" ] && rm -rf "${BASE_DIR}/srcs/gdb-8.2"
	tar zxf gdb-8.2.tar.gz
	cd "${BASE_DIR}/srcs/gdb-8.2"
	./configure --prefix=/usr/local/gdb-8.2 --with-lzma --enable-tui --enable-plugins --enable-profiling --enable-gdbserver --with-python=/usr/bin/python36
	make -j4
	sudo make install -j4
	cd -
}

# https://www.ibm.com/developerworks/cn/linux/1607_caoyq_sysdig/index.html
install_sysdig() {
	curl -s https://s3.amazonaws.com/download.draios.com/stable/install-sysdig | sudo bash
}

yum_packages() {
	sudo yum install -y strace dtrace weighttp \
		nc tree git patch patchelf nmon perf ntpdate \
		tcpdump psmisc zsh gcc gcc-c++
	install_gdb
	sudo yum list installed 'kernel*' | grep -v $(uname -r | sed 's/\.x86_64//g') | awk '{print $1}' | xargs sudo yum remove -y
}

# using the elrepo for the newest mainline kernel
using_ml_kernel() {
	sudo rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
	sudo rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm
	# set to using the new kernel as the default start kernel.
	sudo grub2-set-default 0
}

update_ml_kernel_headers() {
	sudo yum remove -y kernel-headers kernel-tools kernel-tools-libs
	sudo yum --enablerepo=elrepo-kernel install -y kernel-ml-headers kernel-ml-tools kernel-ml-tools-libs
}

update_kernel_from_source() {
	# https://www.kernel.org/doc/Documentation/kbuild/
	local kernel_version="4.18.18"
	local kernel_src_dir="/usr/src/kernels/linux-${kernel_version}"
	sudo yum install -y bc pciutils ncurses-devel util-linux bison flex openssl-devel gcc gcc-c++ glibc-static
	[ -d "/usr/src/kernels/linux-${kernel_version}" ] && sudo rm -rf "/usr/src/kernels/linux-${kernel_version}"
	sudo tar -Jxf "$RUNX_PKGS/linux-${kernel_version}.tar.xz" -C "/usr/src/kernels"
	sudo cp "${PRLCTL_HOME}/scripts/kernel-${kernel_version}.config" "${kernel_src_dir}/.config"
	cd ${kernel_src_dir}
	sudo make -j ${THREADS} clean &&
		sudo make -j ${THREADS} bzImage &&
		sudo make -j ${THREADS} modules &&
		sudo make headers_install &&
		sudo make modules_install &&
		sudo make install &&
		sudo cp ${kernel_src_dir}/.config /boot/config-${kernel_version} &&
		sudo grub2-set-default 0
}

install_systemtap_from_source() {
	cd "${RUNX_PKGS}"
	sudo yum install -y lbzip2 gcc-c++
	sudo tar -xf elfutils-0.170.tar.bz2
	sudo tar -zxf systemtap-4.0.tar.gz
	cd systemtap-4.0
	./configure --with-elfutils=../elfutils-0.170 --prefix=/usr/local/systemtap-4.0
	make -j ${THREADS}
	sudo make install
	cd -
	cd -
}

pre_install_bcc() {
	sudo yum install -y bison ethtool flex git iperf libstdc++-static \
		python-netaddr python-pip gcc gcc-c++ make zlib-devel \
		elfutils-libelf-devel luajit luajit-devel \
		http://dl.marmotte.net/rpms/redhat/el7/x86_64/netperf-2.6.0-1.el7/netperf-2.6.0-1.el7.x86_64.rpm &&
		sudo pip install pyroute2
}

install_bcc_cmake() {
	set -x
	cd "$RUNX_PKGS/"
	tar zxf cmake*
	(cd cmake* && ./bootstrap && gmake && sudo make install)
	cd -
	set +x
}

install_ebpf_ply() {
	set -x
	sudo yum install -y whatprovides autoconf automake
	cp -R "$RUNX_PKGS/ply.bak" "$RUNX_PKGS/ply"
	cd "$RUNX_PKGS/ply"
	./autogen.sh
	export CFLAGS=-I${HOME}/build/usr/include
	./configure
	make
	sudo make install
	set +x
}

install_llvm_clang() {
	set -x
	cd "$RUNX_PKGS/"
	local version=${LLVM_V:-"7.0.0"}
	local build_path="llvm-build-${version}"
	sudo tar -xf cfe-${version}.src.tar.xz &
	sudo tar -xf llvm-${version}.src.tar.xz
	[ ! -z "$(jobs)" ] && sleep 5
	mv cfe-${version}.src llvm-${version}.src/tools
	mkdir ${build_path}
	cd ${build_path}
	cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr/local -DLLVM_TARGETS_TO_BUILD="BPF;X86" -DCMAKE_BUILD_TYPE=Release ../llvm-${version}.src
	make -j ${THREADS}
	sudo make install
	set +x
}

# because of the filesystem issue about 'hard link' so we should make bcc install inner the pvm
install_bcc() {
	set -x
	[ ! -d "/tmp/install-bcc/build-bcc" ] && mkdir -p "/tmp/install-bcc/build-bcc"
	cp -R "$RUNX_PKGS/bcc.bak/" "/tmp/install-bcc/bcc/"
	cd "/tmp/install-bcc/build-bcc"
	cmake -G "Unix Makefiles" -DCMAKE_INSTALL_PREFIX=/usr ../bcc
	make -j ${THREADS}
	sudo make install
	sudo rm -rf "/tmp/install-bcc/build-bcc"
	set +x
}

using_eBPF() {
	pre_install_bcc
	install_bcc_cmake
	install_ebpf_ply
	install_llvm_clang
	install_bcc
}

back_to_iptables() {
	sudo systemctl stop firewalld
	sudo systemctl disable firewalld
	sudo yum install -y iptables-services
	sudo systemctl start iptables
	sudo systemctl enable iptables
}

# HNS="centos7-kube-base-1708-2" /media/psf/runX/deploy/base/init
# init_etc
# update_kernel_from_source
# back_to_iptables
# using_eBPF
# install_systemtap_from_source
# install_gdb
